<?php

namespace App\Http\Controllers;

//use Illuminate\Http\Request;
use Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Models\Book;
use App\Models\Bags;
use App\Models\UsersReg;
use App\Models\UsersAttrib;
use App\Models\BagsLiuyan;

use Session;
use Redirect;

class ShopController extends Controller
{

    public function shop()
    {
        //显示全部商品列表
        //转让方式 -- J借阅，E交换，S出售，Z赠送，B以上均可
        $otype = request::input('otype'); 
        $pre_page = 20;
        if($otype == ''){
            //显示全部商品
            $shoplists = Bags::orderBy('add_time', 'desc')->paginate($pre_page);
        }else{
            //显示转让方式的商品
            $shoplists = Bags::orderBy('add_time', 'desc')->where('otype', $otype)->OrWhere('otype', 'B')->paginate($pre_page);
        }

        foreach($shoplists as $shoplist){
            //对应的图书信息
            $shoplist['books'] = $shoplist->hasOnebooks;
        }

        // foreach($shoplists as $shoplist){
        //     //对应的用户信息
        //     $shoplist['users'] = $shoplist->hasTcUsersReg;
        // }

        // foreach($shoplists as $shoplist)
        // {
        //     //获得对应的出售书籍的留言数量
        //     $bag['liuyancount'] = BagsLiuyan::where('bagsid', $bag->id)->count();
        // }

        //dd($shoplists);

        return view('books.layouts.shop', array(
            'shoplists' => $shoplists,
            'otype'  => $otype
            ));
    }

    public function shopinfo($bagsid)
    {
        //转让图书的信息
        $bags = Bags::where('id', $bagsid)->first();

        //为了得到图书的信息
        $bags['books'] = $bags->hasOnebooks;

        //为了得到商品用户的信息
        $bags['users'] = $bags->hasTcUsersReg;

        if(count($bags) == 0){
            //如果商品信息已经不存在
            abort(404);
        }
        
        //dd($bags);

        //return $bags->users->uid;  
        //return $bags->uid;
        $usersattribs = UsersAttrib::where('uid', $bags->users->uid)->first();
        
        //dd($usersattribs);

        //出售商品的留言列表
        $liuyans = BagsLiuyan::where('bagsid', $bagsid)->orderBy('add_time', 'desc')->get();
        foreach($liuyans as $liuyan)
        {
            //得到留言用户信息
            $liuyan['users'] = $liuyan->hasOneUsersReg;
        }
        
        //return $liuyans;
        //dd($liuyans);

        return view('books.layouts.shopinfo', array(
            'bags'   => $bags,
            'usersattribs' => $usersattribs,
            'liuyans' => $liuyans
            ));
    }

    public function shopsearch()
    {
        //搜索书城
        //显示搜索出来的图书 每页显示的记录数
        $pre_page = 20;

        //获得所有搜索出来的图书信息的数据 按倒序
        $search_text = request::input('search_text');
        $search_text = urldecode($search_text);
        $shoplists = Bags::where('book_name', 'like', '%'.trim($search_text).'%')
                        ->OrWhere('zuozhe', 'like', '%'.trim($search_text).'%')
                        ->OrWhere('isbn', 'like', '%'.trim($search_text).'%')
                        ->orderBy('add_time', 'desc')->orderBy('book_id', 'desc')->paginate($pre_page);

        foreach($shoplists as $shoplist)
        {
            //为了得到图书的信息
            $shoplist['books'] = $shoplist->hasOnebooks;
        }

        // foreach($shoplists as $shoplist)
        // {
        //     //为了得到用户的信息
        //     $shoplist['users'] = $shoplist->hasTcUsersReg;
        // }

        // foreach($shoplist as $shoplist)
        // {
        //     //获得对应的出售书籍的留言数量
        //     $shoplist['liuyancount'] = BagsLiuyan::where('bagsid', $shoplist->id)->count();
        // }


        return view('books.layouts.shopsearch', array(
            'shoplists'   => $shoplists,
            'search_text'  => $search_text
            ));

    }

}
